/**
 * Created by linyanbin on 2018/9/30.
 */
$(function(){
    var loadPageUrl = '/campus/api/articles/p';
    var divEle = $('.blogs');

    loadArticles(1, 5);
    function loadArticles(currPage, size) {
        var pageListEle = $('.blogs.pagelist');
        if (pageListEle.length > 0) {
            // 存在先删除
            pageListEle.remove();
        }
        // 再创建
        var pageListEle = $('<div class="pagelist"></div>');
        $.get(loadPageUrl, {
            'page': currPage,
            'size': size
        }, function (res) {
            divEle.children('li').remove();
            $.each(res.data.list, function (index, article) {
                var liHtml = '<li>' +
                    '<span class="blogpic"><a href="/campus/endless_study/articles/'+ article.articleId +'"><img src="/campus'+ article.navgationImg +'"></a></span>' +
                    '<h3 class="blogtitle"><a href="/campus/endless_study/articles/'+ article.articleId +'">'+ article.articleTitle +'</a></h3>' +
                    '<div class="bloginfo"><p>'+ article.articleSummary +'</p></div>' +
                    '<div class="autor"><span class="dtime">'+ article.createTime +'</span><span class="viewnum">浏览（<a href="composition_content.html">'+ article.articleViewCount +'</a>）</span><span class="readmore"><a href="composition_content.html?'+ article.articleId +'">阅读原文</a></span></div>' +
                    '</li>';
                divEle.append(liHtml);
            })
            // 评论小于5条不显示分页组件
            if (res.data.count <= 5) {
                return;
            }
            divEle.append(pageListEle);
            // 计算上一页，下一页
            var totalPage = res.data.totalPage;
            var prePage =  currPage - 1 > 0 ? currPage - 1 : currPage;
            var nextPage = currPage + 1 <= totalPage ? currPage + 1 : totalPage;
            // 前
            if (currPage == 1) {
                pageListEle.append('<a href="javascript:void(0);" class="last_page disabled" data-page="1">首页</a>' +
                    '<a href="javascript:void(0);" class="previous_page disabled" data-page="'+ prePage +'">&lt;</a>');
            }else {
                pageListEle.append('<a href="javascript:void(0);" class="last_page" data-page="1">首页</a>' +
                    '<a href="javascript:void(0);" class="previous_page" data-page="'+ prePage +'">&lt;</a>');
            }
            // 中
            for (var i = 1; i <= totalPage; i++) {
                if (i == currPage) {
                    pageListEle.append('<a href="javascript:void(0);" class="active" data-page="'+ i +'">'+ i +'</a>');
                    continue;
                }
                pageListEle.append('<a href="javascript:void(0);" data-page="'+ i +'">'+ i +'</a>');
            }
            // 后
            if (currPage == totalPage) {
                pageListEle.append('<a href="javascript:void(0);" class="next_page disabled" data-page="'+ nextPage +'">&gt;</a>' +
                    '<a href="javascript:void(0);" class="last_page disabled" data-page="'+ totalPage +'">末页</a>')
            }else {
                pageListEle.append('<a href="javascript:void(0);" class="next_page" data-page="'+ nextPage +'">&gt;</a>' +
                    '<a href="javascript:void(0);" class="last_page" data-page="'+ totalPage +'">末页</a>')
            }

            // 为分页标签元素添加事件
            var aEles = pageListEle.children();
            for (var i = 0; i < aEles.length; i++) {
                $(aEles[i]).click(function(){
                    var bool = $(this).hasClass('disabled');
                    if (bool) {
                        return;
                    }else {
                        loadArticles($(this).data('page'), 5);
                        window.scrollTo(0, 200);
                    }
                });
            }
        }, "json");
    }
});